package tools

func MapMerge[K comparable, T any](newMap, oldMap map[K]T) map[K]T {
	for key, value := range oldMap {
		newMap[key] = value
	}
	return newMap
}

func MapToSlice[K comparable, T any](targetMap map[K]T, filter func(T) bool) ([]K, []T) {
	var (
		uniqueKeyArray   = make([]K, 0)
		uniqueValueArray = make([]T, 0)
	)

	if len(targetMap) == 0 {
		return nil, nil
	}

	for key, value := range targetMap {
		if filter != nil && filter(value) {
			continue
		}
		uniqueKeyArray = append(uniqueKeyArray, key)
		uniqueValueArray = append(uniqueValueArray, value)
	}

	return uniqueKeyArray, uniqueValueArray
}

func MapCombineAndFilterAndCast[K comparable, T, F any](targetMap map[K]T, filter func(T) bool, cast func(T) []F) ([]K, []F) {
	var (
		uniqueKeyArray   = make([]K, 0)
		uniqueValueArray = make([]F, 0)
	)

	if len(targetMap) == 0 {
		return nil, nil
	}

	for key, value := range targetMap {
		if filter != nil && filter(value) {
			continue
		}
		uniqueKeyArray = append(uniqueKeyArray, key)
		uniqueValueArray = append(uniqueValueArray, cast(value)...)
	}

	return uniqueKeyArray, uniqueValueArray
}
